package com.dong.c06_zero_copy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2020-03-18 21:04
 */
public class NioClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 7001);
        socketChannel.bind(inetSocketAddress);
        socketChannel.configureBlocking(false);
        String fileName="protoc-3.6.1-win32.zip";

        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        long startTime = System.currentTimeMillis();
        //linux下, 一个transferTo可以完成传输
        //win下  , 一次最多8M
        //这个方法就是用的ZeroCopy
        long transerCount = fileChannel.transferTo(0,fileChannel.size(),socketChannel);
        System.out.println("发送的总字节数 : "+transerCount+", 耗时: "+(System.currentTimeMillis()-startTime)+"毫秒");
        fileChannel.close();
    }
}
